package org.jdemo.nosql.zookeeper;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.CuratorWatcher;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.WatchedEvent;
import org.jdemo.util.PropertiesResource;

/**
 * org.jdemo.nosql.zookeeper.ZKListener
 *
 * @author longlin(longlin@cyou-inc.com)
 * @date 2014/9/17
 * @since V1.0
 */
public class ZKListener {
    public static void main(String[] args) throws Exception {
        String zkHost = new PropertiesResource("/zk.properties").getString("zk.host");

        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        final CuratorFramework client = CuratorFrameworkFactory.newClient(zkHost, retryPolicy);
        client.start();

        client.getData().usingWatcher(new CuratorWatcher() {
            @Override
            public void process(WatchedEvent watchedEvent) throws Exception {
                System.out.println(watchedEvent.getPath() + " : " + new String(client.getData().forPath(watchedEvent.getPath())));
                client.getData().usingWatcher(this).forPath("/abc1");
            }
        }).forPath("/abc1");

        Thread.currentThread().join();
    }
}
